//
//  IMSendMessageManager.m
//  TQY
//
//  Created by 甘世清 on 2024/10/29.
//

#import "IMSendMessageManager.h"
static NSMutableDictionary *emjonDict;
@implementation IMSendMessageManager
+(IMSendMessageManager *)sharedIMManager{
    static IMSendMessageManager *imManager;
    @synchronized(self) {
        if (!imManager)
            imManager = [[self alloc] init];
    }
    return imManager;
}
//获取表情
+(NSMutableDictionary *)emjonDict
{
    if (!emjonDict) {
        emjonDict = [[NSMutableDictionary alloc] initWithDictionary:[IMSendMessageManager getTouchEmjonDict]];
    }
    return emjonDict;
}
+(NSMutableDictionary *)getTouchEmjonDict
{
    NSMutableDictionary *_emjonDict = [NSMutableDictionary new];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"expression" ofType:@"plist"];
    NSDictionary *smileDict = [[NSDictionary alloc] initWithContentsOfFile:path];
    [smileDict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        UIImage *immm = [UIImage imageNamed:obj];
        NSData *imageData = UIImagePNGRepresentation(immm);
        YYImage *image = [YYImage imageWithData:imageData scale:2];
        image.preloadAllAnimatedImageFrames = YES;
        [_emjonDict setObject:image forKey:key];
    }];
    
    NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|
    NSWidthInsensitiveSearch|NSForcedOrderingSearch;
    NSComparator sort = ^(NSString *obj1,NSString *obj2){
        NSRange range = NSMakeRange(0,obj1.length);
        return [obj1 compare:obj2 options:comparisonOptions range:range];
    };
    NSArray *imageArr = [smileDict.allValues sortedArrayUsingComparator:sort].copy; //图片名字
    NSMutableArray<QEmotion *> *emotionArray = [[NSMutableArray alloc] init];
    for (NSString * valuse in imageArr) {
        for (NSString * key in smileDict.allKeys) {
            if ([valuse isEqualToString:[smileDict objectForKey:key]]) {
                [emotionArray addObject:[QEmotion emotionWithIdentifier:valuse displayName:key]];
            }
        }
    }
    for (QEmotion *e in emotionArray) {
        e.image = [UIImage imageNamed:e.identifier];
    }
    QEmotionHelper *faceManager = [QEmotionHelper sharedEmotionHelper];
    faceManager.emotionArray = emotionArray;
    
    return _emjonDict;
}


-(void)SQ_sendTextMessage:(NSString*)inputNormalText recvId:(NSString*)recvId{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:recvId forKey:@"recvId"];
    [dic setObject:@"0" forKey:@"type"];
    [dic setObject:inputNormalText forKey:@"content"];
    [HttpTool postJson:@"im/message/private/send" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

/// 创建文字聊天模型
/// - Parameters:
///   - inputNormalText: 文字内容
///   - infoModel: 聊天对象模型
-(IMMessageModel*)SQ_sendTextMessage:(NSString*)inputNormalText info:(PeopleModel*)infoModel{
    IMMessageModel * model = [IMMessageModel new];
    model.content = inputNormalText;
    model.toUser = infoModel.userid;//接收对象
    model.toUserPortrait = infoModel.portrait;
    model.toUserName = infoModel.name;
    model.fromID = [UserModel share].userid;
    model.messType = @"text";
    model.type = @"0";
    model.isSend = YES;//加载的时候还是会二次判断
    model.sendTime = [NSString SQ_currentTime];
    model.isBoom = YES;
    model.needToSend = @"需要发送";
    return model;
}

/// 创建图片聊天模型
/// - Parameters:
///   - inputNormalText: 文字内容
///   - infoModel: 聊天对象模型
-(IMMessageModel*)SQ_sendImageMessage:(UIImage*)image  info:(PeopleModel*)infoModel{
    IMMessageModel * model = [IMMessageModel new];
    model.image = image;
    model.content = @"";
    model.toUser = infoModel.userid;//接收对象
    model.toUserPortrait = infoModel.portrait;
    model.toUserName = infoModel.name;
    model.fromID = [UserModel share].userid;
    model.messType = @"img";
    model.type = @"1";
    model.isSend = YES;//加载的时候还是会二次判断
    model.sendTime = [NSString SQ_currentTime];
    model.isBoom = YES;
    model.needToSend = @"需要发送";
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    if (height < 60) {
        model.imageHeight = 60;
    }
    if (height > SCREEN_WIDETH/3.f) {
        model.imageHeight = SCREEN_WIDETH/3.f;
    }
    model.imageWidth =  (model.imageHeight/height) * width;
    return model;
}
/// 创建定位聊天模型
/// - Parameters:
///   - inputNormalText: 文字内容
///   - infoModel: 聊天对象模型
-(IMMessageModel*)SQ_sendLocationMessage:(NSMutableDictionary*)dic  info:(PeopleModel*)infoModel{
    IMMessageModel * model = [IMMessageModel mj_objectWithKeyValues:dic];
    model.toUser = infoModel.userid;//接收对象
    model.toUserPortrait = infoModel.portrait;
    model.toUserName = infoModel.name;
    model.fromID = [UserModel share].userid;
    model.messType = @"location";
    model.type = @"5";
    model.isSend = YES;//加载的时候还是会二次判断
    model.sendTime = [NSString SQ_currentTime];
    model.isBoom = YES;
    model.needToSend = @"需要发送";
    return model;
}
 

@end
